package com.le.ssm.core.interceptor;

import com.le.ssm.core.constants.CommonConstants;
import com.le.ssm.model.Address;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

    public class AuthInterceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

            //判断请求是否和登录操作相关
            HttpSession session=request.getSession(true);
            // 获取请求需要执行的方法名
           Address loginUser =(Address) session.getAttribute(CommonConstants.LOGIN_USER);
            if (loginUser == null) {
                response.sendRedirect(request.getContextPath() + "/");
                // 终止后续请求
                return false;
            }
            // 放行
            return super.preHandle(request, response, handler);
        }
    }
